package com.wxmit.ot.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;
import java.util.Objects;

/**
 * @Description 默认数据库字段填充类
 * @Author huangxiong
 * @Date 2022/7/8 14:21
 * @Version V1.0
 */
public class DefaultDBFieldHandler implements MetaObjectHandler {


    private static final String CREATE_TIME = "createTime";
    private static final String UPDATE_TIME = "updateTime";

    private static final String CREATE_BY = "createBy";
    private static final String UPDATE_BY = "updateBy";


    @Override
    public void insertFill(MetaObject metaObject) {
        mySetFieldValByName( CREATE_TIME, new Date(),metaObject);
        mySetFieldValByName( UPDATE_TIME, new Date() ,metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        mySetFieldValByName( UPDATE_TIME, new Date() ,metaObject);
    }


    private void mySetFieldValByName(String fieldName,Object fieldVal,MetaObject metaObject){
        if(metaObject.hasSetter(fieldName) && Objects.isNull(metaObject.getValue(fieldName))){
            setFieldValByName( fieldName, fieldVal,metaObject);
        }

    }

}
